package gui_classes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.Iterator;
import java.util.List;

import domain.*;
import java_persistence.*;

/*
 * Panel for System Administrator's home page
 */

public class AdminPanel extends JPanel {
	private static UsersPagePanel upagep;

	
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;
	private JComboBox comboBox_3;
	private JComboBox comboBox_4;

	/**
	 * Create the panel.
	 */
	public AdminPanel(SystemAdministrator admin) {
		setPreferredSize(new Dimension(600, 285));
		setLayout(null);
		GUI_Main.getFrame().setTitle("Logged in as " + admin.getUsername());
		
		List<Doctor> doctors = GUI_Main.getSession().getMapper(DoctorMapper.class).get_doctors();
		List<Nurse> nurses = GUI_Main.getSession().getMapper(NurseMapper.class).get_nurses();
		List<SystemAdministrator> sas = GUI_Main.getSession().getMapper(SystemAdministratorMapper.class).get_sysadmins();
		List<Patient> patients = GUI_Main.getSession().getMapper(PatientMapper.class).get_patients(); // no such method.. why?
		
	  
		int i=0;
		String[] docStr = new String[doctors.size()];
		for ( Iterator<Doctor> doctorsIter = doctors.iterator(); doctorsIter.hasNext(); ) {
			Doctor curd = doctorsIter.next();
			docStr[i]=curd.getName() + " " + curd.getUsername();
			i++;
		}
	//	String[] docStr = {"Dr. Dre", "Dr. House", "Dr. Moriarty"}; // for testing
		
		
		comboBox = new JComboBox(docStr); // add a menu of doctors to combo box
		comboBox.setBounds(12, 74, 124, 24);
		add(comboBox);
		
		JLabel lblDoctors = new JLabel("Doctors");
		lblDoctors.setBounds(60, 43, 70, 15);
		add(lblDoctors);
		
		
		int j=0;
		System.out.println("nurses.size():" + nurses.size());
		String[] nurStr = new String[nurses.size()];
		for ( Iterator<Nurse> nursesIter = nurses.iterator(); nursesIter.hasNext(); ) {
			Nurse curn = nursesIter.next();
			nurStr[j]=curn.getName() + " " + curn.getUsername();
			j++;
		} 
	//	String[] nurStr = {"Nurse Joy", "Nurse Carla"};
		comboBox_1 = new JComboBox(nurStr);
		comboBox_1.setBounds(148, 74, 124, 24);
		add(comboBox_1); 
		
		JLabel lblNurses = new JLabel("Nurses");
		lblNurses.setBounds(170, 43, 70, 15);
		add(lblNurses);
		
		int l=0;
		String[] patStr = new String[patients.size()];
		for( Iterator<Patient> patientsIter = patients.iterator(); patientsIter.hasNext(); ){
			Patient curp = patientsIter.next();
			patStr[l]=curp.getName() + " " + curp.getUsername();
			l++;
		}
		comboBox_2 = new JComboBox(patStr);
		comboBox_2.setBounds(439, 74, 124, 24);
		add(comboBox_2);
		
		JLabel lblPatients = new JLabel("Patients");
		lblPatients.setBounds(476, 43, 70, 15);
		add(lblPatients);
		
		int k=0;
		String[] sasStr = new String[sas.size()];
		for ( Iterator<SystemAdministrator> sasIter = sas.iterator(); sasIter.hasNext(); ) {
			SystemAdministrator curs = sasIter.next();
			sasStr[k]=curs.getName() + " " + curs.getUsername();
			k++;
		}
		comboBox_3 = new JComboBox(sasStr);
		comboBox_3.setBounds(290, 74, 124, 24);
		add(comboBox_3);
		
		JLabel lblAdministrators = new JLabel("Administrators");
		lblAdministrators.setBounds(290, 43, 112, 15);
		add(lblAdministrators);
		
		JButton btnAddUser = new JButton("Add User");
		btnAddUser.setBounds(144, 200, 117, 25);
		btnAddUser.addActionListener(new AddUserListener());
		add(btnAddUser);
		
		JButton btnViewUser = new JButton("View User");
		btnViewUser.setBounds(272, 200, 117, 25);
		btnViewUser.addActionListener(new ViewUserListener());
		add(btnViewUser);
		
		JButton btnSuspendAccount = new JButton("Suspend Account");
		btnSuspendAccount.setBounds(94, 237, 167, 25);
		btnSuspendAccount.addActionListener(new SuspendAccountListener());
		add(btnSuspendAccount);
		
		JButton btnUnsuspendAccount = new JButton("Unsuspend Account");
		btnUnsuspendAccount.setBounds(272, 237, 174, 25);
		btnSuspendAccount.addActionListener(new UnSuspendListener());
		add(btnUnsuspendAccount);
		
		String[] types = {"Patient", "Nurse", "Doctor", "System Administrator"};
		comboBox_4 = new JComboBox(types);
		comboBox_4.setBounds(217, 166, 112, 25);
		add(comboBox_4);
		
		JLabel lblUserType = new JLabel("User Type");
		lblUserType.setBounds(232, 144, 97, 15);
		add(lblUserType);
	}
	
	public JComboBox getDocBox()
	{
		return this.comboBox;
	}
	
	public JComboBox getNurBox(){
		return this.comboBox_1;
	}
	
	public JComboBox getPatBox(){
		return this.comboBox_2;
	}
	
	public JComboBox getAdmBox(){
		return this.comboBox_3;
	}
	
	public JComboBox getTypBox(){
		return this.comboBox_4;
	}

	private class UnSuspendListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Admin has unsuspended a user");

			// This is the name of the user to be viewed.
			String selName = null;
//			boolean isPatient = false;
			String[] names = null;
			String username = null;

			if(((String)comboBox_4.getSelectedItem()).equals("Patient"))
			{
				selName = (String) comboBox_2.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
								
			}
			if(((String)comboBox_4.getSelectedItem()).equals("Nurse"))
			{
				selName = (String) comboBox_1.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];

			}
			if(((String)comboBox_4.getSelectedItem()).equals("Doctor"))
			{
				selName = (String) comboBox.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];

			}

			/* Joseph: go into the database and suspend the user. He will have the 
			 * name, 'selName' */
      
			/*
			 *  I believe this is going to be right.  I'm not at my computer, so I don't have eclipse.  
			 *  if Eclipse suggests a syntax error, it's probably right.  I'll look at it again
			 *  when I'm at my computer.
			 *  
			 *  Also, I don't feel like checking where session is.  All DB stuff needs the correct
			 *  session object.  Also, it's going to take me a minute to finish the changes.
			 */  
  
      
			User u = GUI_Main.getSession().getMapper(UserMapper.class).get_user_by_username(username);

			try {
				GUI_Main.getSession().getMapper(UserMapper.class).ban(u);
				System.out.println(u.getActive_account());
			}catch (Exception e) {
				System.out.println("Caught Exception: " + e.getMessage());
			}
			finally {
				GUI_Main.getSession().commit();
			}
		}
	}
	/*
	 * Listener for add button
	 */
	private class AddUserListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Admin is adding a user");
			GUI_Main.adduserPage = new AddUserPanel();
			GUI_Main.adduserPage.setPrevious(GUI_Main.apage);
			GUI_Main.adduserPage.display();
			
		}
	}
	
	/*
	 * Listener for view user button
	 */
	private class ViewUserListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Admin is viewing a user");
			
			// This is the name of the user to be viewed.
			String selName = null;
//			boolean isPatient = false;
			String[] names = null;
			String username = null;

			if(((String)comboBox_4.getSelectedItem()).equals("Patient"))
			{
				selName = (String) comboBox_2.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
				
				GUI_Main.viewpinfo = new ViewPatientPage(GUI_Main.getSession().getMapper(PatientMapper.class).get_patient_by_username(username), GUI_Main.apage);
				GUI_Main.viewpinfo.setPrevious(GUI_Main.apage);
				GUI_Main.viewpinfo.display();
			}
			if(((String)comboBox_4.getSelectedItem()).equals("Nurse"))
			{
				selName = (String) comboBox_1.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
				
				GUI_Main.viewuinfo = new ViewUserPage(GUI_Main.getSession().getMapper(NurseMapper.class).get_nurse_by_username(username), GUI_Main.apage);
				GUI_Main.viewuinfo.setPrevious(GUI_Main.apage);
				GUI_Main.viewuinfo.display();
			}
			if(((String)comboBox_4.getSelectedItem()).equals("Doctor"))
			{
				selName = (String) comboBox.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
				
				GUI_Main.viewuinfo = new ViewUserPage(GUI_Main.getSession().getMapper(DoctorMapper.class).get_doctor_by_username(username), GUI_Main.apage);
				GUI_Main.viewuinfo.setPrevious(GUI_Main.apage);
				GUI_Main.viewuinfo.display();
			}
			if(((String)comboBox_4.getSelectedItem()).equals("System Administrator"))
			{
				selName = (String) comboBox_3.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
	
	// Uncomment after fixed			
				GUI_Main.viewuinfo = new ViewUserPage(GUI_Main.getSession().getMapper(SystemAdministratorMapper.class).get_sa_by_username(username), GUI_Main.apage);
				GUI_Main.viewuinfo.setPrevious(GUI_Main.apage);
				GUI_Main.viewuinfo.display();
			}
		}
	}
	
	/*
	 * Listener for suspend account button
	 */
	private class SuspendAccountListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Admin has suspended a user");

			// This is the name of the user to be viewed.
			String selName = null;
//			boolean isPatient = false;
			String[] names = null;
			String username = null;

			if(((String)comboBox_4.getSelectedItem()).equals("Patient"))
			{
				selName = (String) comboBox_2.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];
								
			}
			if(((String)comboBox_4.getSelectedItem()).equals("Nurse"))
			{
				selName = (String) comboBox_1.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];

			}
			if(((String)comboBox_4.getSelectedItem()).equals("Doctor"))
			{
				selName = (String) comboBox.getSelectedItem();
				System.out.println(selName);
				names = selName.split(" ");
				username = names[names.length-1];

			}
			// Can't suspend System Administrator
			if(((String)comboBox_4.getSelectedItem()).equals("System Administrator"))
			{

				JOptionPane.showMessageDialog(null, "You can not suspend another administrator"); 

			} 
			/* Joseph: go into the database and suspend the user. He will have the 
			 * name, 'selName' */
      
			/*
			 *  I believe this is going to be right.  I'm not at my computer, so I don't have eclipse.  
			 *  if Eclipse suggests a syntax error, it's probably right.  I'll look at it again
			 *  when I'm at my computer.
			 *  
			 *  Also, I don't feel like checking where session is.  All DB stuff needs the correct
			 *  session object.  Also, it's going to take me a minute to finish the changes.
			 */  
  
      
			User u = GUI_Main.getSession().getMapper(UserMapper.class).get_user_by_username(username);

			try {
				GUI_Main.getSession().getMapper(UserMapper.class).ban(u);
				System.out.println(u.getActive_account());
			}catch (Exception e) {
				System.out.println("Caught Exception: " + e.getMessage());
			}
			finally {
				GUI_Main.getSession().commit();
			}
      
		}
	}
}